package com.example.demo.controller;

import com.example.demo.bean.PurchaseOrderDetail;
import com.example.demo.bean.ResBody;
import com.example.demo.service.PurchaseOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class PurchaseOrderDetailController {
    @Autowired
    PurchaseOrderDetailService service;

    @PostMapping("/api/addPurchaseOrderDetail")
    public ResBody addPurchaseOrderDetail(@RequestBody PurchaseOrderDetail purchaseOrderDetail){
        ResBody resBody = new ResBody();
        int i = service.addPurchaseOrderDetail(purchaseOrderDetail);
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("添加成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("添加失败");
        }
        return resBody;
    }

    @PostMapping("/api/updatePurchaseOrderDetail")
    public ResBody updatePurchaseOrderDetail(@RequestBody PurchaseOrderDetail detail){
        ResBody resBody = new ResBody();
        int i = service.updatePurchaseOrderDetail(detail);
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("修改成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("修改失败");
        }
        return resBody;
    }

    @GetMapping("/api/deletePurchaseOrderDetail")
    public ResBody deletePurchaseOrderDetail(Integer id){
        ResBody resBody = new ResBody();
        int i = service.deletePurchaseOrderDetail(id);
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("删除成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("删除失败");
        }
        return resBody;
    }

    @GetMapping("/api/getAllPurchaseOrderDetail")
    public ResBody getAllPurchaseOrderDetail(@RequestParam int page,
                                                @RequestParam int limit) {
        ResBody resBody = new ResBody();
        int count = service.getCount();
        List<PurchaseOrderDetail> list= service.getAllPurchaseOrderDetail(page, limit);
        resBody.setCount(count);
        resBody.setData(list);
        resBody.setCode(0);
        return resBody;
    }

    @GetMapping("/api/findPurchaseOrderDetailByName")
    public ResBody findPurchaseOrderDetailByName(@RequestParam int page,
                                           @RequestParam int limit,
                                           @RequestParam String name){
        System.out.println("name: " + name);
        int count = 0;
        List<PurchaseOrderDetail> list= new ArrayList<>();
        ResBody resBody = new ResBody();
        if (name.isEmpty()){
            count = service.getCount();
            list= service.getAllPurchaseOrderDetail(page, limit);
        }else {
            list = service.findPurchaseOrderDetailByName(name);
        }
        resBody.setCount(count);
        resBody.setData(list);
        resBody.setCode(0);
        return resBody;
    }

    @GetMapping("/api/findPurchaseOrderDetailById")
    public ResBody findPurchaseOrderDetailById(@RequestParam int page,
                                                    @RequestParam int limit,
                                                    @RequestParam String order_id){
        int count = 0;
        List<PurchaseOrderDetail> list = new ArrayList<>();
        ResBody resBody = new ResBody();
        if (!order_id.isEmpty()){
            count = service.getCount();
            list= service.getAllPurchaseOrderDetail(page, limit);
        }else {
            count = service.getCount(Integer.parseInt(order_id));
            list= service.findAllPurchaseOrderDetailById(page, limit, Integer.parseInt(order_id));
        }
        resBody.setCount(count);
        resBody.setData(list);
        resBody.setCode(0);
        return resBody;
    }

    @PostMapping("/api/updateTotalAmount")
    public ResBody updateTotalAmount(@RequestBody String order_id){
        ResBody resBody = new ResBody();
        String numberOnly = order_id.replaceAll("\\D", ""); // 使用正则表达式替换非数字字符为空
        int i = service.updateTotalAmount(Integer.parseInt(numberOnly));
        if (i == 1){
            resBody.setCode(200);
            resBody.setMsg("修改成功");
        }else{
            resBody.setCode(500);
            resBody.setMsg("修改失败");
        }
        return resBody;
    }

    @GetMapping("/ajax/getAllOrderProducts")
    public ResBody getAllOrderProducts(@RequestParam int order_id) {
        ResBody resBody = new ResBody();
        List<PurchaseOrderDetail> list = service.getAllOrderProducts(order_id);
        resBody.setData(list);
        resBody.setCode(0);
        return resBody;
    }

}
